using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using  Material.Object;
using Material.Entity;
using System.Configuration;

namespace Material.App
{
    public partial class FrmPenjBarangDTL : Form
    {
        public static int IdBarang;
        public static int IdTransDTL;
        public static int IdTransPembDTL;

        //private TblTransPemBarangDTL tblTransPemBarangDTL;
        //private TblTransPemBarangDTLEntity tblTransPemBarangDTLEntity;

        private TblBarang tblBarang;
        private TblBarangEntity tblBarangEntity;

        public static DateTime tglJual = DateTime.Now;

        private TblTransPenjBarangHDR tblTransPenjBarangHDR;
        private TblTransPenjBarangHDREntity tblTransPenjBarangHDREntity;

        private TblTransPenjBarangDTL tblTransPenjBarangDTL;
        private TblTransPenjBarangDTLEntity tblTransPenjBarangDTLEntity;

        public static bool InsertHDR = false;
        public static bool InsertDTL = false;
        public static DateTime TglJual = DateTime.Now;
        public static string IdTransHDR;

        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();

        public FrmPenjBarangDTL()
        {
            InitializeComponent();
        }

        private void FrmPenjBarangDTL_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                FrmFindBarang frm = new FrmFindBarang();
                frm.ShowDialog();

                tblBarang = new TblBarang();
                tblBarangEntity = new TblBarangEntity(connStr);
                tblBarang.Id = IdBarang;
                tblBarang = tblBarangEntity.GetDataByID(tblBarang);

                txtNamaBarang.Text = tblBarang.NamaBarang;
                txtKelompok.Text = tblBarang.TblKelompok.Kelompok;
                txtSatuan.Text = tblBarang.TblSatuan.Satuan;

                //tblTransPemBarangDTL = new TblTransPemBarangDTL();
                //tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);
                //tblTransPemBarangDTL.IdTransDTL = IdTransPembDTL;
                //tblTransPemBarangDTL = tblTransPemBarangDTLEntity.GetDataByID(tblTransPemBarangDTL);

                txtHarga1.Text = tblBarang.Harga1.ToString();
                txtHarga2.Text = tblBarang.Harga2.ToString();
                txtHarga3.Text = tblBarang.Harga3.ToString();
                jmlstock.Text = tblBarang.Jumlah.ToString();

                txtJumlah.Focus();
            }
            catch
            {
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = 0;
            if (txtNamaBarang.Text.Trim().Length == 0)
            {
                MessageBox.Show("Kolom Nama Barang Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtNamaBarang.Focus();
                return;
            }
            else
            {
             
                 tblTransPenjBarangDTL = new TblTransPenjBarangDTL();
                 tblTransPenjBarangDTLEntity = new TblTransPenjBarangDTLEntity(connStr);

                 tblTransPenjBarangDTL.IdBarang = IdBarang;
                 tblTransPenjBarangDTL.IdTransHDR = FrmPenjBarangHRD.idHDR;
                 tblTransPenjBarangDTL.TglJual = DateTime.Now;
                 tblTransPenjBarangDTL.IdBarang = IdBarang;
                 tblTransPenjBarangDTL.Jumlah = Convert.ToInt32(txtJumlah.Text);

                 if(rbHarga1.Checked)
                     tblTransPenjBarangDTL.HargaJual = Convert.ToDecimal(txtHarga1.Text);
                 if (rbHarga2.Checked)
                     tblTransPenjBarangDTL.HargaJual = Convert.ToDecimal(txtHarga2.Text);
                 if (rbHarga3.Checked)
                     tblTransPenjBarangDTL.HargaJual = Convert.ToDecimal(txtHarga3.Text);

                

                 tblTransPenjBarangDTL.Diskon = Convert.ToInt32(txtDiskon.Text);
                 tblTransPenjBarangDTL.TotalHarga = (Convert.ToDecimal(txtTotal.Text))  - (Convert.ToDecimal(txtTotal.Text) * Convert.ToDecimal(txtDiskon.Text)) / 100;

                 tblTransPenjBarangDTL.CreatedDate = DateTime.Now;
                 tblTransPenjBarangDTL.CreatedBy = FrmLogin.UserID;
                 tblTransPenjBarangDTL.ModifiedDate = DateTime.Now;
                 tblTransPenjBarangDTL.ModifiedBy = FrmLogin.UserID;

                 i = tblTransPenjBarangDTLEntity.Insert(tblTransPenjBarangDTL);

                 
                 if (i != -1)
                 {
                     MessageBox.Show("Data berhasil disimpan...","Pesan",MessageBoxButtons.OK,MessageBoxIcon.Information);

                     tblBarang = new TblBarang();
                     tblBarangEntity = new TblBarangEntity(connStr);

                     tblBarang.Jumlah = Convert.ToInt32(jmlstock.Text) - Convert.ToInt32(txtJumlah.Text);
                     tblBarang.Id = IdBarang;
                     tblBarang.ModifiedBy = FrmLogin.UserID;
                     tblBarang.ModifiedDate = DateTime.Now;


                     tblBarangEntity.UpdateJmlBarang(tblBarang);

                     this.Close();
                 }
                 else
                     MessageBox.Show("Data tidak berhasil disimpan...", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }



           
        }

        private void txtJumlah_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                    //int jumlah = Convert.ToInt32(txtJumlah.Text);

                    //tblTransPemBarangDTL = new TblTransPemBarangDTL();
                    //tblTransPemBarangDTLEntity = new TblTransPemBarangDTLEntity(connStr);

                    //tblTransPemBarangDTL.IdTransDTL = IdTransDTL;
                    //tblTransPemBarangDTL = tblTransPemBarangDTLEntity.GetDataByID(tblTransPemBarangDTL);
                    //if (Convert.ToInt32(txtJumlah.Text) > tblTransPemBarangDTL.Jumlah)
                    //{
                    //MessageBox.Show("Jumlah yang diinput lebih besar dari stok barang sekarang,stok barang sekarang : " + tblTransPemBarangDTL.Jumlah.ToString(), "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //txtJumlah.Text = "0";

                    //}
                    //else
                    //{

                    //if (rbHarga1.Checked)
                    //    txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga1.Text) * Convert.ToDecimal(txtJumlah.Text));

                    //if (rbHarga2.Checked)
                    //    txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga2.Text) * Convert.ToDecimal(txtJumlah.Text));

                    //if (rbHarga3.Checked)
                    //    txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga3.Text) * Convert.ToDecimal(txtJumlah.Text));

                    //}
        }
           
        }

        private void rbHarga1_CheckedChanged(object sender, EventArgs e)
        {
            if(rbHarga1.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga1.Text) * Convert.ToDecimal(txtJumlah.Text) - ((Convert.ToDecimal(txtHarga1.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);
        }

        private void rbHarga2_CheckedChanged(object sender, EventArgs e)
        {
            if (rbHarga2.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga2.Text) * Convert.ToDecimal(txtJumlah.Text) - ((Convert.ToDecimal(txtHarga2.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);
        }

        private void rbHarga3_CheckedChanged(object sender, EventArgs e)
        {
            if (rbHarga3.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga3.Text) * Convert.ToDecimal(txtJumlah.Text) - ((Convert.ToDecimal(txtHarga3.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);
        }

        private void txtDiskon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //if (rbHarga1.Checked)
                //    txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga1.Text) * Convert.ToDecimal(txtJumlah.Text)-((Convert.ToDecimal(txtHarga1.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);

                //if (rbHarga2.Checked)
                //    txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga2.Text) * Convert.ToDecimal(txtJumlah.Text) - ((Convert.ToDecimal(txtHarga2.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);

                //if (rbHarga3.Checked)
                //    txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga3.Text) * Convert.ToDecimal(txtJumlah.Text) - ((Convert.ToDecimal(txtHarga3.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);
            }
        }

        private void txtHarga1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtHarga2_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtHarga3_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtJumlah_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;


        }

        private void txtDiskon_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtJumlah_MouseLeave(object sender, EventArgs e)
        {
            
            

        }

        private void txtJumlah_TextChanged(object sender, EventArgs e)
        {
            if (Convert.ToInt32(txtJumlah.Text) > Convert.ToInt32(jmlstock.Text))
            {
                MessageBox.Show("Jumlah yang diinput lebih besar dari stok barang sekarang,stok barang sekarang : " + jmlstock.Text, "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtJumlah.Text = "0";

            }


            if (rbHarga1.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga1.Text) * Convert.ToDecimal(txtJumlah.Text));

            if (rbHarga2.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga2.Text) * Convert.ToDecimal(txtJumlah.Text));

            if (rbHarga3.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga3.Text) * Convert.ToDecimal(txtJumlah.Text));

        }

        private void txtDiskon_TextChanged(object sender, EventArgs e)
        {
            if (rbHarga1.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga1.Text) * Convert.ToDecimal(txtJumlah.Text) - ((Convert.ToDecimal(txtHarga1.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);

            if (rbHarga2.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga2.Text) * Convert.ToDecimal(txtJumlah.Text) - ((Convert.ToDecimal(txtHarga2.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);

            if (rbHarga3.Checked)
                txtTotal.Text = Convert.ToString(Convert.ToDecimal(txtHarga3.Text) * Convert.ToDecimal(txtJumlah.Text) - ((Convert.ToDecimal(txtHarga3.Text) * Convert.ToDecimal(txtJumlah.Text)) * Convert.ToDecimal(txtDiskon.Text)) / 100);

        }
        
    }
}